package com.example;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

/**
 * HTML解析处理类
 * 用于解析书签HTML文件
 */
public class HtmlProcessor {
    
    /**
     * 解析HTML文件
     * @param filePath HTML文件路径
     * @return Document对象
     * @throws IOException IO异常
     */
    public Document parseHtmlFile(String filePath) throws IOException {
        File htmlFile = new File(filePath);
        return Jsoup.parse(htmlFile, "UTF-8");
    }
    
    /**
     * 解析HTML字符串
     * @param html HTML字符串
     * @return Document对象
     */
    public Document parseHtmlString(String html) {
        return Jsoup.parse(html);
    }
    
    /**
     * 提取所有链接元素
     * @param doc HTML文档
     * @return 链接元素集合
     */
    public Elements extractLinks(Document doc) {
        return doc.select("a[href]");
    }
    
    /**
     * 提取所有文件夹元素(H3标签)
     * @param doc HTML文档
     * @return H3元素集合
     */
    public Elements extractFolders(Document doc) {
        return doc.select("h3");
    }
    
    /**
     * 打印所有书签信息
     * @param doc HTML文档
     */
    public void printBookmarks(Document doc) {
        Elements links = doc.select("a");
        System.out.println("找到 " + links.size() + " 个书签:");
        
        for (Element link : links) {
            String url = link.attr("href");
            String title = link.text();
            String addDate = link.attr("add_date");
            
            System.out.println("标题: " + title);
            System.out.println("链接: " + url);
            System.out.println("添加日期: " + addDate);
            System.out.println("-----------------------");
        }
        
        Elements folders = doc.select("h3");
        System.out.println("找到 " + folders.size() + " 个文件夹:");
        
        for (Element folder : folders) {
            String folderName = folder.text();
            String addDate = folder.attr("add_date");
            
            System.out.println("文件夹名称: " + folderName);
            System.out.println("添加日期: " + addDate);
            System.out.println("-----------------------");
        }
    }
}